/** FASSIT (Forms of Selective Attention in Intelligent Transportation Systems)
 * Computational Creativity and Digital Media
 * Cognitive and Media Systems Group
 * Centre for Informatics and Systems of the University of Coimbra (CISUC)
 *
 * Copyright (c) 2010-2013 University of Coimbra, Portugal
 * All rights reserved.
 */
package poi.Info;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;

import functionsVariables.Constants;

/**
 * This class is used to store a HashMap of Days when the POI is open. A DayOn
 * is composed by a Day an its Schedule
 * 
 * @author Hernani Costa
 * @version 0.1 date: 05/11/2012
 */
public class DaysOn implements Serializable {
	private static final long serialVersionUID = 1L;
	private HashMap<String, Day> listDaysOn;
	private ArrayList<String> days_on;

	/**
	 * ------------------------------------------------------------------------
	 * Constructor class
	 * ------------------------------------------------------------------------
	 */
	public DaysOn() {
		listDaysOn = new HashMap<String, Day>();

	}

	/**
	 * ------------------------------------------------------------------------
	 * Constructor class - Creates automatically the DaysOn, however all DaysOn
	 * will have the same Schedule
	 * ------------------------------------------------------------------------
	 * 
	 * @param daysOff
	 *            - DaysOff
	 * @param schedule
	 *            - Schedule for the daysOn
	 */
	public DaysOn(DaysOff daysOff, Schedule schedule) {
		this();
		Day d = null;
		ArrayList<String> days_Off = daysOff.getListDaysOff();
		// creating the DaysOn automatically, through the daysOff
		for (String day : Constants.getStrDays()) {
			if (!days_Off.contains(day)) {
				d = new Day(day, schedule);
				this.addDayOn(d);
			}
		}
	}

	/**
	 * ------------------------------------------------------------------------
	 * Adds a new Day - useful for specify individual schedules for each dayOn
	 * ------------------------------------------------------------------------
	 * 
	 * @param dayName
	 *            - name of the Day
	 * @param day
	 *            - day when the POI is open
	 */
	public void addDayOn(Day day) {
		listDaysOn.put(day.getDayName(), day);
	}

	/**
	 * 
	 * @param dayName
	 *            - removes from the list of DaysOn the Day with the name
	 *            received in the input
	 */
	public void removeDayOn(String dayName) {
		listDaysOn.remove(dayName);
		removeDayOnFromList(dayName);
	}

	private void removeDayOnFromList(String dayName) {
		ArrayList<String> temp = new ArrayList<String>(days_on);
		String day="";
		for(int i=0 ; i<temp.size(); i++){
			day= temp.get(i);
			if(day.equals(dayName)){
				days_on.remove(i);
				return;
			}
		}
	}
	// ------------------------------------------------------------------------
	// ------------------------GETs and SETs-----------------------------------
	// ------------------------------------------------------------------------

	/**
	 * 
	 * @return a HashMap<String,Day> with a list of days when the POI is open
	 */
	public HashMap<String, Day> getListDaysOn() {
		return listDaysOn;
	}

	/**
	 * 
	 * @return a ArrayList<String> with a list of days when the POI is open
	 */
	public ArrayList<String> getArrayListDaysOn() {
		days_on = new ArrayList<String>();
		for (String day : listDaysOn.keySet())
			days_on.add(day);
		return days_on;
	}

	public boolean isOpenThisDayOfTheWeek(String dayName) {
		for (String dName : days_on)
			if (dName.equals(dayName))
				return true;
		return false;
	}
	
	/**
	 * ------------------------------------------------------------------------
	 * Returns the list of DaysON in a String, splitted by commas
	 * ------------------------------------------------------------------------
	 * 
	 * @return a String with all the daysON, splitted by commas
	 */
	public String getStringDaysOn() {
		String temp = "";
		for(Day d: getListDaysOn().values()){
			temp += d.getDayName()+d.getSchedule().to_String() + ",";
		}
		if (temp.length() > 1)
			temp = temp.substring(0, temp.length() - 1);
		return temp.trim();
	}

	/**
	 * 
	 * @return a String with all the Open Days, splitted by commas
	 */
	public String to_String() {
		String temp = "\n\t";
		if (listDaysOn.size() > 0) {
			temp = "[ ";
		}

		for (String day : listDaysOn.keySet()) {
			temp = temp + day + "="
					+ listDaysOn.get(day).getSchedule().to_String() + ", ";
		}
		temp = temp.substring(0, temp.length() - 2);
		temp += " ]";

		return temp;
	}
}
